package com.fangcang.mongodb;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fangcang.mongodb.model.Account;
import com.fangcang.mongodb.model.Customer;
import com.mongodb.gridfs.GridFSDBFile;

@RestController
@RequestMapping("/customer")
public class CustomerController {

	@Autowired
    private CustomerRepository customerRepository;
	
	@Resource(name="mongoDbService")
	private MongoDbService<Customer> mongoDbService;
	
    @RequestMapping(value = "/insertCustomer", method = RequestMethod.GET)
    public Customer insertCustomer() {
        Customer customer = new Customer();
        customer.setFirstname("李");
        customer.setSecondname("四");
        
        List<Account> accountList = new LinkedList<Account>();
        Account acount = new Account();
        acount.setAccountId("6258000011112222");
        acount.setAccountName("平安信用卡");
        acount.setDepositeMoney(Double.valueOf(2000000));
        accountList.add(acount);
        
        customer.setAccountList(accountList);
        mongoDbService.save(customer);
        return customer;
    }
	
	

    @RequestMapping(value = "/addCustomer", method = RequestMethod.GET)
    public Customer addCustomer(@RequestParam("firstname") String firstname,
                                @RequestParam("secondname") String secondname) {
        Customer customer = new Customer();
        customer.setFirstname(firstname);
        customer.setSecondname(secondname);
        customer.setMemo("testssfsfsf");
        
        List<Account> accountList = new LinkedList<Account>();
        Account acount = new Account();
        acount.setAccountId("6258000011112222");
        acount.setAccountName("平安信用卡");
        acount.setDepositeMoney(Double.valueOf(2000000));
        accountList.add(acount);
        
        customer.setAccountList(accountList);
        return customerRepository.save(customer);
    }

    @RequestMapping(value = "/getAllCustomer", method = RequestMethod.GET)
    public List<Customer> getAllCustomer() {
        return customerRepository.findAll();
    }

    @RequestMapping(value = "/getCustomerByFirstname", method = RequestMethod.GET)
    public Customer getCustomerByFirstname(@RequestParam("firstname") String firstname) {
        return customerRepository.findByFirstname(firstname);
    }

    @RequestMapping(value = "/getCustomerBySecondname", method = RequestMethod.GET)
    public List<Customer> getCustomerBySecondname(@RequestParam("secondname") String secondname) {
        return customerRepository.findBySecondname(secondname);
    }

    @RequestMapping(value = "/deleteCustomerById", method = RequestMethod.GET)
    public boolean deleteCustomerById(@RequestParam("cid") String cid) {
        customerRepository.deleteById(cid);
        return true;
    }
    
    
    @RequestMapping(value = "/saveFile", method = RequestMethod.GET)
    public boolean saveFile() {
    	mongoDbService.SaveFile("test", new File("d://22.jpg"), "22.jpg",  "12345678900", "1");
        return true;
    }
    
    @RequestMapping(value = "/listFile", method = RequestMethod.GET)
    public boolean listFile() {
    	Map<String, String> map = new HashMap<String, String>();
//    	map.put("collectionName", "test");
    	map.put("hotelId", "12345678900");
    	map.put("imageType", "2");
    	List<GridFSDBFile> list = mongoDbService.retrieveFileList("test", map);
    	if(null != list) {
    		list.forEach(file -> {
    			try {
    				System.out.println(file.getFilename());
					file.writeTo(new File("d://a" + file.getFilename()));
				} catch (IOException e) {
					e.printStackTrace();
				}
    		});
    	}
        return true;
    }
}
